from rest_framework.views import APIView
from tools.lib.util import Util
from rest_framework.parsers import FormParser, MultiPartParser
from datetime import datetime
import random
from django.conf import settings
from tools.lib.ocr import ocr
from tools.models import Photo
from tools.lib.upload import UploadPhoto

class OcrView(APIView): 

    parser_classes = (MultiPartParser, FormParser) 

    def get(self, request, format=None): 
        return Util.say_yes({"text":"Welcome!"})

    def post(self, request, format=None): 
        data = request.data
        if Util.check_data_key(data,["image"]):
            result = UploadPhoto.save_photo(data["image"])
            if not result[1]:
                return Util.say_no({"text":"图片上传失败"})
            text = ocr(result[1])
            return Util.say_yes({'text':text})
        else:
            return Util.say_no({"text":"Usage: {'image':XXXXXXXXXXX.jpg'}"})
